package cn.mrlong.base.utils

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import cn.mrlong.base.R
import com.google.android.material.snackbar.Snackbar
import java.lang.ref.WeakReference


/**
 * 作者：BINGO
 * 时间：2021/03/29 14:09
 */
class SnackbarUtils private constructor() {
    companion object {
        private var snackbarWeakReference: WeakReference<Snackbar>? = null
        fun show(
            parent: View,
            message: String,
            duration: Int = Snackbar.LENGTH_SHORT,
            textColor: Int = Color.WHITE,
            backColor: Int = Color.GRAY,
            actionText: String? = null,
            actionTextColor: Int = Color.WHITE,
            listener: View.OnClickListener? = null
        ) {
            snackbarWeakReference = when (duration) {
                Snackbar.LENGTH_INDEFINITE -> {
                    WeakReference(Snackbar.make(parent, message, Snackbar.LENGTH_INDEFINITE))
                }
                else -> {
                    WeakReference(Snackbar.make(parent, message, duration))
                }
            }
            val view = snackbarWeakReference?.get()?.view
            (view?.findViewById<View>(R.id.snackbar_text) as TextView).setTextColor(textColor)
            view?.setBackgroundColor(backColor)
            try {
                val layoutParams = view?.layoutParams;
                // 重新设置属性参数
                val cl = FrameLayout.LayoutParams(layoutParams.width, layoutParams.height);
                // 设置显示位置在上居中
                cl.gravity = Gravity.CENTER
                view.layoutParams = cl
            } catch (e: Exception) {

            }
            if (null != actionText && actionText.length > 0 && null != listener) {
                snackbarWeakReference?.get()?.setActionTextColor(actionTextColor)
                snackbarWeakReference?.get()?.setAction(actionText, listener)
            }
            snackbarWeakReference?.get()?.show()
        }

        fun addView(layout: Int, index: Int) {
            val snackbar = snackbarWeakReference?.get()
            if (null != snackbar) {
                val view = snackbar.view
                val snackLayout: Snackbar.SnackbarLayout = view as Snackbar.SnackbarLayout
                val child = LayoutInflater.from(view.context).inflate(layout, null)
                val params = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                params.gravity = Gravity.CENTER_VERTICAL
                snackLayout.addView(child, index, params)
            }
        }

        fun dismiss() {
            if (null != snackbarWeakReference && null != snackbarWeakReference?.get()) {
                snackbarWeakReference?.get()?.dismiss()
                snackbarWeakReference = null
            }
        }


    }
}